
// method  方法   router  跳转的路由   data  需要传递的参数
const pageChange = (method, route, data) => {
	if (uni.getStorageInfoSync('beforePage')) {
		const beforePage = getCurrentPages()[getCurrentPages().length - 1].route;
		let current = beforePage.split('/');
		let routeCurrent = route.split('/');
		let backNum = routeCurrent.reduce((obj, cur) => {
			(cur in obj) ? obj[cur]++ : obj[cur] = 1;
			return obj
		}, {})['..'];
		routeCurrent = current.splice(0,current.length -  backNum-1 ).concat(routeCurrent.slice(backNum)).join('/');
		uni.setStorageSync('beforePage', beforePage);
		uni.setStorageSync('currentPage', routeCurrent);
	}
	// 普通页面跳转
	if (method == 'navigateTo') {
		if(data){
			uni.navigateTo({
				url: route+'?data='+ JSON.stringify(data) 
			});
		}else{
			uni.navigateTo({
				url: route
			});
		}
	}
	// 关闭当前页面,跳转
	if (method == 'redirectTo') {
		console.log('是否调用这里了', data)
		if(data){
			uni.redirectTo({
				url: route+'?data='+ JSON.stringify(data) 
			});
		}else{
			uni.redirectTo({
				url: route
			});
		}
	}
	// 关闭所有页面直接跳转
	if (method == 'reLaunch') {
		uni.reLaunch({
			url: route
		});
	}
	// 跳转tabBar页面
	if (method == 'switchTab') {
		uni.switchTab({
			url: route
		});
	}
};
 
export default pageChange;
